<script src="/js-test-resources/js-test.js"></script>
<iframe id='iframe'></iframe>
<script>
description("This test is to test whether preload image will require 2 more requests or not.");

window.jsTestIsAsync = true;

function CallCommand(cmd)
{
   try {
       var req = new XMLHttpRequest;
       req.open("GET", "/resources/network-simulator.php?command=" + cmd, false);
       req.send(null);
       return req.responseText;
   } catch (ex) {
       return "";
   }
}

var count;
function checkAccessCount()
{
   // Get the access count of resource:/loading/resources/preload-test.jpg. Should be 1.
   count = CallCommand("get-resource-count&path=/loading/resources/preload-test.jpg");
   shouldBeEqualToString('count', '1');
   finishJSTest();
}

// Reset resource count   
CallCommand("reset-resource-count");

window.addEventListener("message", function(event) {
    if (event.data == 'imgerror') {
        testFailed("test img load failed");
        finishJSTest();
    } else if (event.data == 'imgload') {
        setTimeout(checkAccessCount, 1000);
    } else {
        debug(event.data); 
    }
});

var iframe = document.getElementById("iframe");
iframe.src = 'resources/preload-img-test-iframe.html';
</script>
